// pages/adviser/mybuilding/mybuilding.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag:0,
    code_id:0,
    adviser_id:0,
    houseList:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,1)
    var code_id = 0;
    if (options.scene == undefined) {
      var flag = 1;
      this.setData({flag:1})
      var adviser_id = options.adviser_id
    } else {
      var flag = 0;
      var scene = decodeURIComponent(options.scene);
      scene = scene.split('_')

      if (scene.length > 1) {
        code_id = scene[1]
      }
      var adviser_id = scene[0];

    }

    this.setData({adviser_id:adviser_id, code_id: code_id})
    app.core('get', 'getAllProjectByAdviserIdAndCode', { adviser_id: adviser_id,flag:flag, code_id: code_id}, (res) => {
      let houseList = []
      if (res.data) {
        houseList = res.data
        houseList.map((res, index) => {
          houseList[index]['selected'] = 1
        })
      }
      this.setData({
        houseList: houseList,
        code:res.codeUrl
      })
      console.log(this.data.houseList)
    })
  },

  /**
   * 删除绑定楼盘
   */
  bindDelete(e){
    var index = e.currentTarget.dataset.index;
    var newhouse_project_id = e.currentTarget.dataset.id;
    var adviser_id = this.data.adviser_id;
    var houseList = this.data.houseList;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认删除吗？',
      success(res) {
        if (res.confirm) {

          app.core('get', 'deleteAdviserProject', { adviser_id: adviser_id, newhouse_project_id: newhouse_project_id }, (res) => {
            houseList.splice(index, 1);
            that.setData({ houseList: houseList})
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })


  },

  handleSwitch(e) {
    console.log(e)
    return false
  },

  handleChangeSwitch(e) {
    console.log(e)
    return false
  },

/**
 * 下载楼盘码
 */
  bindLoading(){
    var adviser_id = this.data.adviser_id
    wx.navigateTo({
      url: '../mybuildingPoster/mybuildingPoster?adviser_id=' + adviser_id,
    })
    // var code = this.data.code;
    // wx.downloadFile({
    //   url: code, //仅为示例，并非真实的资源
    //   success(res) {
    //     wx.saveImageToPhotosAlbum({
    //       filePath: res.tempFilePath,
    //       success(res) {
    //           wx.showToast({
    //             title: '保存成功',
    //           })
    //       },
    //       fail(res){

    //       }
    //     })
    //   }
    // })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindUnSelect: function (e) {
    let houseList = this.data.houseList
    houseList.map((res, index) => {
      if (res.id == e.target.dataset.id) {
        houseList[index]['selected'] = 0
      }
    })

    this.setData({ houseList: houseList})
  },

  bindSelect: function (e) {
    let houseList = this.data.houseList
    houseList.map((res, index) => {
      if (res.id == e.target.dataset.id) {
        houseList[index]['selected'] = 1
      }
    })

    this.setData({ houseList: houseList})
  },

  submit: function (e) {
    let houseList = this.data.houseList
    if (houseList.length == 0) {
      return false
    }

    let id = []
    houseList.map((res, index) => {
      if (res.selected == 1) {
        id.push(res.id)
      }
    })

    var adviser_id = this.data.adviser_id

    app.core('post', 'user/adviserHouseCode', { adviser_id: adviser_id, newhouse_id: id }, (res) => {
      this.setData({
        code: res.data.code
      })

      wx.navigateTo({
        url: '../mybuildingPoster/mybuildingPoster?adviser_id=' + adviser_id + '&code=' + encodeURIComponent(res.data.code),
      })
    })

  }

})
